

/***************************************************************************
* HEADER-FILES (Only those that are needed in this file)
****************************************************************************/
/* System-headerfiles */


/* Own headerfiles */
#include  "gear1c1.h"                          
#include  "../adapt/gear1ct.h"                               
#include  "../adapt/gear1ci.h"                                  

/***************************************************************************
* GLOBAL DEFINITIONS
*
* In this section define
* - all global ROM-constants of your module
* - all global variables of your module
****************************************************************************/
DPOOL_txGEAR_tstGearData DPOOL_txGEAR_stGearData = { False, False, 0 };
DPOOL_txGEAR_tstShiftSymbolData  DPOOL_txGEAR_stShiftSymbolData = { 0 };



void GEAR_vMain( void )
{
    // 1. 读取输入信号
    uint8 drvModDisp = GEAR__u8GetDrvModDisp();
    uint8 displayGear = GEAR__u8GetDisplayGear();
    uint8 flashingSignal = GEAR__u8GetFlashingSignal();
    uint8 shiftRecommend = GEAR__u8GetShiftRecommend();
    uint8 tgtgearUp = GEAR__u8GetTgtgear_Up();
    uint8 tgtgearDown = GEAR__u8GetTgtgear_Down();

    // 2. 超时处理（>10周期显示为空，恢复后立即恢复正常）
    static uint8 shiftRecommendTimeoutCnt = 0;
    static uint8 tgtgearUpTimeoutCnt = 0;
    static uint8 tgtgearDownTimeoutCnt = 0;
    const uint8 TIMEOUT_CYCLE = 10;

    if (Flg_ShiftRecommend_M_tmout()) {
        if (shiftRecommendTimeoutCnt < TIMEOUT_CYCLE) shiftRecommendTimeoutCnt++;
    } else {
        shiftRecommendTimeoutCnt = 0;
    }
    if (Flg_Tgtgear_Up_tmout()) {
        if (tgtgearUpTimeoutCnt < TIMEOUT_CYCLE) tgtgearUpTimeoutCnt++;
    } else {
        tgtgearUpTimeoutCnt = 0;
    }
    if (Flg_Tgtgear_Down_tmout()) {
        if (tgtgearDownTimeoutCnt < TIMEOUT_CYCLE) tgtgearDownTimeoutCnt++;
    } else {
        tgtgearDownTimeoutCnt = 0;
    }

    // 3. 挡位显示与闪烁
    // 这里只做简单映射，实际项目可根据DisplayGear和FlashingSignal组合映射到GEAR_tenGearState
    DPOOL_txGEAR_stGearData.GEAR_boGearValid = (shiftRecommendTimeoutCnt < TIMEOUT_CYCLE);
    DPOOL_txGEAR_stGearData.GEAR_boGearFlashSts = (flashingSignal == 1);
    DPOOL_txGEAR_stGearData.GEAR_u8GearPosi = displayGear;

    // 4. 换挡提示符（上下箭头，闪烁2秒后常亮）
    // 1Hz闪烁，2秒后常亮，假设25ms周期，每40次为1秒
    static uint16 shiftSymbolFlashCnt = 0;
    static uint8 shiftSymbolState = 0; // 0:无 1:上 2:下
    static uint8 shiftSymbolFlashPhase = 0; // 0:闪烁 1:常亮
    const uint16 FLASH_2S = 80; // 2秒

    if (tgtgearUp && !Flg_Tgtgear_Up_tmout()) {
        shiftSymbolState = 1;
        shiftSymbolFlashCnt = 0;
        shiftSymbolFlashPhase = 0;
    } else if (tgtgearDown && !Flg_Tgtgear_Down_tmout()) {
        shiftSymbolState = 2;
        shiftSymbolFlashCnt = 0;
        shiftSymbolFlashPhase = 0;
    } else if (shiftSymbolState != 0) {
        shiftSymbolFlashCnt++;
        if (shiftSymbolFlashCnt >= FLASH_2S) {
            shiftSymbolFlashPhase = 1; // 2秒后常亮
        }
    }
    // 超时或无信号，清空
    if ((Flg_Tgtgear_Up_tmout() && Flg_Tgtgear_Down_tmout()) || (tgtgearUp == 0 && tgtgearDown == 0)) {
        shiftSymbolState = 0;
        shiftSymbolFlashCnt = 0;
        shiftSymbolFlashPhase = 0;
    }
    // 更新DPOOL
    if (shiftSymbolState == 1) {
        if (shiftSymbolFlashPhase == 0) {
            // 闪烁
            DPOOL_txGEAR_stShiftSymbolData.GEAR_u8ShiftSymbolPosi = (shiftSymbolFlashCnt / 20) % 2 ? 1 : 0; // 1Hz
        } else {
            DPOOL_txGEAR_stShiftSymbolData.GEAR_u8ShiftSymbolPosi = 1; // 常亮
        }
    } else if (shiftSymbolState == 2) {
        if (shiftSymbolFlashPhase == 0) {
            DPOOL_txGEAR_stShiftSymbolData.GEAR_u8ShiftSymbolPosi = (shiftSymbolFlashCnt / 20) % 2 ? 2 : 0; // 1Hz
        } else {
            DPOOL_txGEAR_stShiftSymbolData.GEAR_u8ShiftSymbolPosi = 2; // 常亮
        }
    } else {
        DPOOL_txGEAR_stShiftSymbolData.GEAR_u8ShiftSymbolPosi = 0;
    }
}

void GEAR_vInit( void )
{
    printf("Gear Init\n");
}



/***************************************************************************
* EOF: GEAR1C1.C
****************************************************************************/
